import matplotlib.pyplot as plt
import numpy as np

# 中文显示
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei','DV Sans']
plt.rcParams['axes.unicode_minus'] = False

def extreme_value_theorem():
    x = np.linspace(-np.pi, np.pi, 1000)
    f = lambda x: x*np.sin(3*x) + np.cos(x)  # 复杂连续函数
    
    plt.figure(figsize=(12, 6))
    plt.plot(x, f(x), 'b-', linewidth=2, label='f(x) = x·sin(3x) + cos(x)')
    
    # 寻找最大值和最小值点
    max_idx = np.argmax(f(x))
    min_idx = np.argmin(f(x))
    
    x_max, y_max = x[max_idx], f(x)[max_idx]
    x_min, y_min = x[min_idx], f(x)[min_idx]
    
    plt.scatter([x_max, x_min], [y_max, y_min], 
               color=['red', 'green'], s=100, 
               label=[f'最大值点 ({x_max:.2f}, {y_max:.2f})', 
                      f'最小值点 ({x_min:.2f}, {y_min:.2f})'])
    
    plt.title('最值存在定理演示')
    plt.xlabel('x'); plt.ylabel('f(x)')
    plt.legend(); plt.grid(True, alpha=0.3)
    plt.show()

extreme_value_theorem()